package it.unipd.dei.bd.icineasti.database;

import it.unipd.dei.bd.icineasti.resource.Palinsesto;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/** 
  * 
  * Aggiorna o inserisce nel database le informazioni sul palinsesto
  * @author Coccato Mattia
  */

public class CreaOAggiornaPalinsestoDatabase {
	
	private static final String STATEMENT_INSERT = "INSERT INTO Palinsesto (codiceevento, dataora, in3d) VALUES (?, ?, ?);";
	private static final String STATEMENT_UPDATE = "UPDATE Palinsesto SET codiceevento = ?, dataora = ?, in3d = ? WHERE codice = ?;";
	private static final String STATEMENT_DELETE = "DELETE FROM Palinsesto WHERE codice = ?;";
	

	private final Connection con;

	private final Palinsesto palinsesto;

	public CreaOAggiornaPalinsestoDatabase (Connection con, Palinsesto palinsesto) {
		this.con = con;
		this.palinsesto = palinsesto;
	}

	public void inserisciPalinsesto() throws SQLException {

		PreparedStatement pstmt = null;

		try {
			pstmt = con.prepareStatement(STATEMENT_INSERT);

			pstmt.setInt(1, palinsesto.getCodiceEvento());
			pstmt.setTimestamp(2, palinsesto.getDataOra());
			pstmt.setBoolean(3, palinsesto.getIsIn3d());

			pstmt.execute();

		} finally {
			if (pstmt != null) {
				pstmt.close();
			}

			con.close();
		}


	}

	public void aggiornaPalinsesto() throws SQLException {

		PreparedStatement pstmt = null;

		try {
			
			pstmt = con.prepareStatement(STATEMENT_UPDATE);


			pstmt.setInt(1, palinsesto.getCodiceEvento());
			pstmt.setTimestamp(2, palinsesto.getDataOra());
			pstmt.setBoolean(3, palinsesto.getIsIn3d());
			
			pstmt.setInt(4, palinsesto.getCodice());

			pstmt.execute();

		} finally {
			if (pstmt != null) {
				pstmt.close();
			}

			con.close();
		}

	}

/*
	public void eliminaPalinsesto() throws SQLException {

		PreparedStatement pstmt = null;

		try {
			
			pstmt = con.prepareStatement(STATEMENT_DELETE);
			
			pstmt.setInt(1, palinsesto.getCodice());

			pstmt.execute();

		} finally {
			if (pstmt != null) {
				pstmt.close();
			}

			con.close();
		}

	}
*/
}
